// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   OutboundWapSIMessage.java

package cn.sendsms;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Date;

// Referenced classes of package cn.sendsms:
//			OutboundMessage, SMSLibException, MessageEncodings, MessageTypes, 
//			WapSISignals

public class OutboundWapSIMessage extends OutboundMessage
{

	private static final long serialVersionUID = 1L;
	private static final String PDU_PATTERN = "29060603AE81EA8DCA02056A0045C6_URL1_03_URL2_00080103_TEXT_000101";
	private String siPdu;
	protected URL url;
	protected Date createDate;
	protected Date expireDate;
	protected WapSISignals signal;
	private static final String protocolBytes[][] = {
		{
			"http://www.", "0D"
		}, {
			"https://www.", "0F"
		}, {
			"http://", "0C"
		}, {
			"https://", "0E"
		}
	};
	private static final String domainBytes[][] = {
		{
			".com/", "85"
		}, {
			".edu/", "86"
		}, {
			".net/", "87"
		}, {
			".org/", "88"
		}
	};

	public OutboundWapSIMessage(String recipient, URL url, Date createDate, Date expireDate, WapSISignals signal, String text)
		throws SMSLibException
	{
		siPdu = "";
		this.url = url;
		this.createDate = new Date(createDate.getTime());
		this.expireDate = new Date(expireDate.getTime());
		this.signal = signal;
		setText(text);
		setEncoding(MessageEncodings.ENCUCS2);
		setSrcPort(9200);
		setDstPort(2948);
		setType(MessageTypes.WAPSI);
		setRecipient(recipient);
		try
		{
			fixPdu();
		}
		catch (UnsupportedEncodingException e)
		{
			throw new SMSLibException(e.getMessage());
		}
	}

	public OutboundWapSIMessage(String recipient, URL url, String text)
		throws SMSLibException
	{
		this(recipient, url, new Date(), new Date(), WapSISignals.NONE, text);
	}

	public Date getCreateDate()
	{
		return new Date(createDate.getTime());
	}

	public void setCreateDate(Date createDate)
	{
		this.createDate = new Date(createDate.getTime());
	}

	public Date getExpireDate()
	{
		return new Date(expireDate.getTime());
	}

	public void setExpireDate(Date expireDate)
	{
		this.expireDate = new Date(expireDate.getTime());
	}

	public WapSISignals getSignal()
	{
		return signal;
	}

	public void setSignal(WapSISignals signal)
	{
		this.signal = signal;
	}

	public URL getUrl()
	{
		return url;
	}

	public void setUrl(URL url)
	{
		this.url = url;
	}

	protected String getPDUData()
	{
		return siPdu;
	}

	private String bytesToHexStr(byte b[])
	{
		if (b == null)
			return "";
		StringBuffer strBuffer = new StringBuffer(b.length * 3);
		for (int i = 0; i < b.length; i++)
			strBuffer.append(Integer.toHexString(b[i] & 0xff));

		return strBuffer.toString();
	}

	private void fixPdu()
		throws UnsupportedEncodingException
	{
		siPdu = "29060603AE81EA8DCA02056A0045C6_URL1_03_URL2_00080103_TEXT_000101";
		String s = bytesToHexStr(getText().getBytes("UTF-8"));
		siPdu = siPdu.replaceAll("_TEXT_", s);
		boolean foundProtocol = false;
		String urlText = url.toString();
		int i = 0;
		do
		{
			if (i >= 4)
				break;
			if (urlText.indexOf(protocolBytes[i][0]) == 0)
			{
				foundProtocol = true;
				siPdu = siPdu.replaceAll("_URL1_", protocolBytes[i][1]);
				urlText = urlText.replaceAll(protocolBytes[i][0], "");
				break;
			}
			i++;
		} while (true);
		if (!foundProtocol)
			siPdu = siPdu.replaceAll("_URL1_", "0B");
		s = "";
		for (i = 0; i < urlText.length(); i++)
		{
			boolean foundDomain = false;
			String subUrl = urlText.substring(i);
			int j = 0;
			do
			{
				if (j >= 4)
					break;
				if (subUrl.indexOf(domainBytes[j][0]) == 0)
				{
					foundDomain = true;
					i += 4;
					s = s + "00";
					s = s + domainBytes[j][1];
					s = s + "03";
					break;
				}
				j++;
			} while (true);
			if (!foundDomain)
			{
				char c = urlText.charAt(i);
				s = s + (Integer.toHexString(c).length() >= 2 ? Integer.toHexString(c) : "0" + Integer.toHexString(c));
			}
		}

		siPdu = siPdu.replaceAll("_URL2_", s);
		setEncodedText(siPdu);
	}

}
